package demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.StandardServletEnvironment;

import demo.service.MqttServerStart;
import io.moquette.parser.proto.messages.AbstractMessage.QOSType;
import io.moquette.parser.proto.messages.PublishMessage;
import io.moquette.server.Server;

@SpringBootApplication
@Controller
public class SpringApp {
	@Resource
	private MqttServerStart mqttServerStart; 
	
	@Resource
	private StandardServletEnvironment environment;
	
	@RequestMapping("test")
	public void test(HttpServletResponse response){
		try {
			PrintWriter writer = response.getWriter();
			writer.print("<pre>");
			environment.getSystemProperties().forEach((String k,Object v)->{
				writer.println(k+"\t\t:\t\t"+v);
				
			});
			System.out.println("-----------------------------------");
			environment.getSystemEnvironment().forEach((String k,Object v)->{
				writer.println(k+"\t\t:\t\t"+v);
			});
			writer.print("</pre>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("mqtt")
	@ResponseBody
	public Object viewMqtt(@RequestParam(required=false,defaultValue="0")int v){
		int[]  rawData1=null;
		if(v==0){
			//关机
			System.out.println("关机");
			rawData1 = new int[] {1050,10850, 1050,2850, 1050,2900, 1050,2900, 1000,6800, 1000,6800, 1000,2900, 1050,2900, 1000,2900, 1050,41100, 1000,10850, 1000,2950, 1000,2900, 1050,6750, 1000,2950, 1000,2900, 1050,6750, 1050,6750, 1050,6750, 1000,41150, 1000,10850, 1000,6800, 1000,6800, 1000,6800, 1000,6800, 1050,2900, 1000,2900, 1050,2900, 1000,2900, 1050,41100, 1000,10850, 1050,6750, 1050,2900, 1000,2900, 1050,2900, 1000,6800, 1000,6850, 950,6850, 950,2950, 1000,41150, 950,10900, 1000,2950, 950,6850, 950,2950, 1000,6850, 950,2950, 1000,2950, 950,3000, 950,2950, 950,41150, 950}; 
		}else if(v==1){
			System.out.println("开机");
			//开机
			rawData1 = new int[]  {1000,10850, 1050,2900, 1000,2900, 1050,2850, 1050,6750, 1050,6750, 1000,2900, 1050,2900, 1000,2900, 1050,40950, 1050,10850, 1000,2900, 1000,2900, 1050,6750, 1050,2850, 1050,2900, 1000,6750, 1050,6750, 1050,6750, 1050,40950, 1050,10800, 1050,6750, 1050,6750, 1050,6750, 1000,6750, 1050,2900, 1000,2900, 1000,2950, 1000,2900, 1000,41050, 1000,10850, 1000,6750, 1050,2900, 1000,2900, 1000,2950, 1000,2900, 1000,6800, 1000,6750, 1050,6750, 1050,41000, 1000,10850, 1000,2900, 1000,6800, 1000,2900, 1000,6800, 950,2950, 1000,2950, 1000,6800, 950,2950, 1000,41050, 950};  // UNKNOWN C8025FD8
		}else{
			rawData1=new int[]  {1000,10900, 1000,2900, 1050,2900, 1000,2900, 1050,6750, 1050,6750, 1050,2900, 1000,2900, 1050,2900, 1050,41050, 1000,10850, 1050,2900, 1050,2850, 1050,6750, 1050,2900, 1050,2850, 1050,6750, 1050,6750, 1050,6750, 1050,41050, 1050,10850, 1000,6800, 1000,6800, 1000,6800, 1000,6800, 1000,2900, 1050,2900, 1000,2900, 1050,2900, 1000,41050, 1050,10850, 1050,6750, 1000,2900, 1050,2900, 1000,2900, 1050,6750, 1000,6800, 1000,2950, 1000,6800, 950,41100, 1000,10900, 1000,2950, 950,6850, 950,2950, 1000,6800, 1000,2950, 950,2950, 1000,6850, 950,2950, 950,41100 };
			
			System.out.println("调试");
		}
		
		ByteBuffer buffer=ByteBuffer.allocate(2*(rawData1.length+2));
		
		
		
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		short data=1;
		buffer.putShort(data);
		buffer.putShort((short)rawData1.length);
		
		for (int i = 0; i < rawData1.length; i++) {
			data=(short) rawData1[i];
			if(data<0){
				data-=Short.MIN_VALUE;
			}
			buffer.putShort(data);
		}
		//System.out.println(Arrays.toString(rawData1));
		
//		
//		
//		if(s==null)
//			s="default";
//		
//		buffer.flip();
//		
		Server server=mqttServerStart.getServer();
		
		PublishMessage msg=new PublishMessage();
		msg.setClientId("aaaaaaaaaaa");
		msg.setTopicName("test/lol");
		msg.setQos(QOSType.LEAST_ONE);
		msg.setPayload(buffer);
		server.internalPublish(msg);
		
		
		return server.getSubscriptions();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(SpringApp.class, args);
	}
}
